let express = require('express')
let router = express.Router()
let mysql = require('mysql')

let conn = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'qqq123',
  port: '3306',
  database: 'test'
})

router.get("/query", (req, res, next) => {
  conn.connect();
  const sql = "select * from stu"; // 执行查询语句
  conn.query(sql, (err, result) => {
    if (err) {
      res.json({
        code: 500,
        msg: "SQL执行错误",
        err
      })
    } else {
      res.json({
        code: 200,
        msg: "查询成功",
        data: result
      })
    }
    conn.destroy();
  })
})

module.exports = router